﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using Zlib.Zip;

namespace Server.TCP
{
    public class CompressItem
    {
        public string Name { get; set; }
        public byte[] Stream { get; set; }
    }

    public static class ZipHelper
    {
        public static byte[] CreateZipFileStream(IEnumerable<CompressItem> streams)
        {
            byte[] array = null;
            try
            {
                MemoryStream zipStream = new MemoryStream();
                using (ZipOutputStream s = new ZipOutputStream(zipStream))
                {
                    s.SetLevel(9);
                    byte[] buffer = new byte[4096];

                    foreach (CompressItem item in streams)
                    {
                        ZipEntry entry = new ZipEntry(item.Name);
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        s.Write(item.Stream, 0, item.Stream.Length);
                    }

                    s.Finish();
                    array = SerializeHelper.ReadFully(zipStream);
                    s.Close();
                }
            }
            catch (Exception ex)
            {
            }
            return array;
        }

        public static IEnumerable<CompressItem> UncompressZipFile(byte[] file)
        {
            List<CompressItem> zipList = new List<CompressItem>();
            try
            {
               
                MemoryStream fileStream  = new MemoryStream(file);
                using (ZipInputStream s = new ZipInputStream(fileStream))
                {
                    ZipEntry theEntry = null;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    ms.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            ms.Flush();
                            byte[] bytes = SerializeHelper.ReadFully(ms);
                            ms.Close();
                            zipList.Add(new CompressItem() { Name = theEntry.Name, Stream = bytes });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return zipList;
        }

        public static IEnumerable<CompressItem> UncompressZipFile(string zipFile)
        {
            List<CompressItem> zipList = new List<CompressItem>();
            try
            {
                FileInfo fileInfo = new FileInfo(zipFile);
                using (ZipInputStream s = new ZipInputStream(fileInfo.OpenRead()))
                {
                    ZipEntry theEntry = null;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    ms.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            ms.Flush();
                            byte[] bytes = SerializeHelper.ReadFully(ms);
                            ms.Close();
                            zipList.Add(new CompressItem() { Name = theEntry.Name, Stream = bytes });
                        }
                    } 
                }
            }
            catch (Exception ex)
            {
            }
            return zipList;
        }
    }

}
